﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.DAL;
using System.Data.SqlClient;
using JATProject.DataAccess;
using ThaiHoa.Win.QLSX.Kho;

namespace ThaiHoa.Win.QLSX.QLKH.Business
{
    public partial class LenKeHoach : Form
    {
        static string conStr = ConnectDB.GetStringConnection();
        static SqlConnection con = new SqlConnection(conStr);
        SqlCommand cmd = con.CreateCommand();
        private KhoDBContext db;
        
        public LenKeHoach()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();


                cmd.CommandText = "insert into ProductPlan (CodeProductPlan,OrderID,ProducttivityAmount) values (@CodeProductPlan,@OrderID,@ProducttivityAmount)";
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@CodeProductPlan", textBox7.Text.Trim());
                cmd.Parameters.AddWithValue("@OrderID", comboBox3.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@ProducttivityAmount", textBox2.Text.Trim());
              
                cmd.ExecuteNonQuery();
                MessageBox.Show("Success!");
                textBox7.ReadOnly = true;
                textBox2.ReadOnly = true;
                comboBox3.Enabled = false;
                button1.Enabled = true;
                button2.Enabled = false;
            }
            catch
            {
                MessageBox.Show("Error!");
            }
            finally
            {
                con.Close();
            }
        }

        private void LenKeHoach_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'jATDBDataSet1.Order' table. You can move, or remove it, as needed.
            this.orderTableAdapter.Fill(this.jATDBDataSet1.Order);
            //dat che do an cho tao moi nguyen cong
            button1.Enabled = false;

            db = new KhoDBContext();
            List<DayChuyen> listDayChuyen = db.DayChuyen.Where(d => d.TrangThai).ToList();
            comboBox2.DataSource = listDayChuyen;

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //SqlCommand cmd1 = con.CreateCommand();
            try
            {
                
                SqlDataReader dr;
                con.Open();
                cmd.CommandText = "SELECT * FROM [Order] WHERE OrderID = '" + comboBox3.SelectedValue.ToString() + "'";
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    this.textBox2.Text = dr["ProductAmount"].ToString();

                }
            }
            catch {
               
            }

            finally
            {
                con.Close();
            }
              

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

  
    }
}
